using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
public class HL_Admin_Admin_LabelTemplate : Admin, IRequiresSessionState
{
	private LabelBll _lb = null;
	protected HtmlGenericControl closeBar;
	protected HtmlGenericControl bar;
	protected Literal ParmList;
	protected TextBox LabelName;
	protected Button ShowButton2;
	protected Button ShowButton3;
	protected Literal ShowPlace;
	protected Button closeB;
	protected Panel Panel1;
	protected TextBox TemplateContent;
	protected DropDownList LabelClassSelect;
	protected Literal SysLabel;
	protected Literal ParmLabel;
	protected Button PreStepButton;
	protected Button FinishButton;
	protected Button CreateLabelButton;
	protected Panel Panel2;
	protected LabelBll lb
	{
		get
		{
			LabelBll lb;
			if (this._lb == null)
			{
				this._lb = new LabelBll(this.conn);
				lb = this._lb;
			}
			else
			{
				lb = this._lb;
			}
			return lb;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			this.BindPage();
			this.BingSysLabel();
			DataTable labelType = this.lb.GetLabelType();
			this.LabelClassSelect.DataSource = labelType;
			this.LabelClassSelect.DataTextField = "Type";
			this.LabelClassSelect.DataValueField = "Type";
			this.LabelClassSelect.DataBind();
			this.LabelName.Text = "{HL.Label ID=\"" + base.Request["name"] + "\" }";
			this.listParameter();
		}
	}
	protected void BingSysLabel()
	{
		Literal expr_07 = this.SysLabel;
		expr_07.Text = expr_07.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"SiteName\">网站名称</div>" + Environment.NewLine;
		Literal expr_28 = this.SysLabel;
		expr_28.Text = expr_28.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"Logo\">网点Logo</div>" + Environment.NewLine;
		Literal expr_49 = this.SysLabel;
		expr_49.Text = expr_49.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"InstallDir\">站点安装路径</div>" + Environment.NewLine;
		Literal expr_6A = this.SysLabel;
		expr_6A.Text = expr_6A.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"Banner\">网点Banner</div>" + Environment.NewLine;
		Literal expr_8B = this.SysLabel;
		expr_8B.Text = expr_8B.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"WebMaster\">站长名</div>" + Environment.NewLine;
		Literal expr_AC = this.SysLabel;
		expr_AC.Text = expr_AC.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"WebMasterEmail\">站长名Email</div>" + Environment.NewLine;
		Literal expr_CD = this.SysLabel;
		expr_CD.Text = expr_CD.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"SiteUrl\">网站Url</div>" + Environment.NewLine;
		Literal expr_EE = this.SysLabel;
		expr_EE.Text = expr_EE.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"PageTitle\">当前页面标题</div>" + Environment.NewLine;
		Literal expr_10F = this.SysLabel;
		expr_10F.Text = expr_10F.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"UserCookies\">曾经登录用户名</div>" + Environment.NewLine;
		Literal expr_130 = this.SysLabel;
		expr_130.Text = expr_130.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"LogingUserName\">当前登录用户名</div>" + Environment.NewLine;
		Literal expr_151 = this.SysLabel;
		expr_151.Text = expr_151.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"LogingUserID\">当前登录用户ID</div>" + Environment.NewLine;
		Literal expr_172 = this.SysLabel;
		expr_172.Text = expr_172.Text + "<div onclick=\"cit()\" onmousedown=\"dit(6)\" class=\"label_square\" value=\"CopyRight\">版权信息</div>" + Environment.NewLine;
	}
	public void listParameter()
	{
		string text;
		if (Fetch.Get("action") == "add" && !string.IsNullOrEmpty(Fetch.Get("name")))
		{
			DataTable dataTable = (DataTable)this.Session[Fetch.Get("name")];
			text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>";
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				for (int j = 0; j < dataTable.Columns.Count; j++)
				{
					text = string.Concat(new string[]
					{
						text,
						"<",
						dataTable.Columns[j].ColumnName.ToString(),
						">",
						dataTable.Rows[i][j].ToString(),
						"</",
						dataTable.Columns[j].ColumnName.ToString(),
						">\n"
					});
				}
			}
			text += "</root>";
			text = text.Replace("<ParmArray>", "");
			text = text.Replace("</ParmArray>", "");
		}
		else
		{
			DataTable labelByLabelName = this.lb.GetLabelByLabelName(base.Request["name"]);
			if (labelByLabelName.Rows.Count <= 0)
			{
				this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
			}
			text = labelByLabelName.Rows[0]["Define"].ToString();
			if (text == null || text == "")
			{
				this.WriteErrMsg("", "错误的标签！", "");
			}
		}
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.LoadXml(text);
		XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/parms");
		this.ParmList.Text = "";
		if (xmlNodeList != null)
		{
			foreach (XmlNode xmlNode in xmlNodeList)
			{
				XmlNode xmlNode2 = xmlNode.SelectSingleNode("name");
				XmlNode xmlNode3 = xmlNode.SelectSingleNode("intro");
				if (xmlNode2 == null || xmlNode3 == null)
				{
					this.WriteErrMsg("", "错误的标签！", "");
				}
				Literal expr_2D7 = this.ParmList;
				expr_2D7.Text += "<div style=\"width:100%; text-align:left;\">\n";
				Literal expr_2F3 = this.ParmList;
				expr_2F3.Text = expr_2F3.Text + "    <div style=\"float:left; width:15%; text-align:right; line-height:25px;\">" + xmlNode3.InnerXml + "：</div>\n";
				Literal expr_31B = this.ParmList;
				string text2 = expr_31B.Text;
				expr_31B.Text = string.Concat(new string[]
				{
					text2,
					"    <div style=\"float:left;\"><input onblur=\"return SetParam(this)\" id=\"",
					xmlNode2.InnerXml,
					"\" name=\"",
					xmlNode2.InnerXml,
					"\"/></div>\n"
				});
				Literal expr_38D = this.ParmList;
				expr_38D.Text += "</div>\n";
			}
		}
		XmlNode xmlNode4 = xmlDocument.SelectSingleNode("/root/EnablePage");
		if (xmlNode4 != null)
		{
			string innerXml = xmlNode4.InnerXml;
			if (innerXml == "1")
			{
				Literal expr_418 = this.ParmList;
				expr_418.Text += "<div style=\"width:100%; text-align:left;\">\n";
				Literal expr_434 = this.ParmList;
				expr_434.Text += "    <div style=\"float:left; width:15%; text-align:right; line-height:25px;\">当前页：</div>\n";
				Literal expr_450 = this.ParmList;
				expr_450.Text += "    <div style=\"float:left;\"><input id=\"Page\" name=\"Page\"/></div>\n";
				Literal expr_46C = this.ParmList;
				expr_46C.Text += "</div>\n";
				Literal expr_488 = this.ParmList;
				expr_488.Text += "<div style=\"width:100%; text-align:left;\">\n";
				Literal expr_4A4 = this.ParmList;
				expr_4A4.Text += "    <div style=\"float:left; width:15%; text-align:right; line-height:25px;\">每页显示数：</div>\n";
				Literal expr_4C0 = this.ParmList;
				expr_4C0.Text += "    <div style=\"float:left;\"><input onblur=\"return SetParam(this)\" id=\"MaxPerPage\" name=\"MaxPerPage\"/></div>\n";
				Literal expr_4DC = this.ParmList;
				expr_4DC.Text += "</div>\n";
			}
		}
	}
	protected void BindPage()
	{
		this.TemplateContent.Attributes.Add("onmouseup", "dragend();");
		this.TemplateContent.Attributes.Add("onmousemove", "movePoint();");
		if (!string.IsNullOrEmpty(Fetch.Get("action")))
		{
			DataTable dataTable = new DataTable();
			if (Fetch.Get("action") == "add")
			{
				string text = "";
				if (this.Session["Parm_Table_" + base.Request["name"]] != null)
				{
					dataTable = (DataTable)this.Session["Parm_Table_" + Fetch.Get("name")];
					text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine;
					text = text + "<xsl:transform version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:hl=\"labelproc\" exclude-result-prefixes=\"hl\">" + Environment.NewLine;
					text = text + "<xsl:output method=\"html\" />" + Environment.NewLine;
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						string text2 = text;
						text = string.Concat(new string[]
						{
							text2,
							"<xsl:param name=\"",
							dataTable.Rows[i]["name"].ToString(),
							"\" />",
							Environment.NewLine
						});
					}
					text = text + "<xsl:template match=\"/NewDataSet/Table\">" + Environment.NewLine;
					text = text + "\t\t在此输入模板代码！" + Environment.NewLine;
					text = text + "</xsl:template>" + Environment.NewLine;
					text = text + "</xsl:transform>" + Environment.NewLine;
				}
				this.TemplateContent.Text = text;
				this.CreateLabelButton.Text = "请先保存";
				this.CreateLabelButton.Enabled = false;
			}
			else
			{
				if (Fetch.Get("action") == "modify" && Fetch.GetID("ID") > 0)
				{
					int iD = Fetch.GetID("ID");
					DataTable labelByLabelID = this.lb.GetLabelByLabelID(iD);
					if (labelByLabelID.Rows.Count == 1)
					{
						this.TemplateContent.Text = labelByLabelID.Rows[0]["Template"].ToString();
						dataTable = this.lb.GetParmDataTable(labelByLabelID.Rows[0]["Define"].ToString());
						this.CreateLabelButton.Text = "更新静态标签页";
					}
					else
					{
						this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
					}
				}
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				foreach (DataRow dataRow in dataTable.Rows)
				{
					Literal expr_33A = this.ParmLabel;
					string text2 = expr_33A.Text;
					expr_33A.Text = string.Concat(new string[]
					{
						text2,
						"<div onclick=\"cit()\" onmousedown=\"dit(4)\" class=\"label_square\" value=\"<xsl:value-of select=&qout;$",
						dataRow["name"].ToString(),
						"&qout; />\">",
						dataRow["intro"].ToString(),
						"</div>"
					});
				}
			}
			else
			{
				this.ParmLabel.Text = "你还没有添加作何参数<a href=\"Admin_LabelParms.aspx?action=add&name=" + base.Request["name"] + "\" class=\"label_square\">添加标签</a>";
			}
		}
	}
	protected void select_LabelClassSelect(object sender, EventArgs e)
	{
		this.SysLabel.Text = "";
		if (this.LabelClassSelect.SelectedValue == "系统标签")
		{
			this.BingSysLabel();
		}
		else
		{
			DataTable labelByType = this.lb.GetLabelByType(this.LabelClassSelect.SelectedValue);
			if (labelByType.Rows.Count > 0)
			{
				foreach (DataRow dataRow in labelByType.Rows)
				{
					Literal expr_95 = this.SysLabel;
					string text = expr_95.Text;
					expr_95.Text = string.Concat(new string[]
					{
						text,
						"<div title=\"",
						dataRow["Name"].ToString(),
						"\" onclick=\"cit()\" onmousedown=\"dit(7)\" class=\"label_square\" value=\"",
						dataRow["Name"].ToString(),
						"\">"
					});
					if (dataRow["Name"].ToString().Length > 8)
					{
						Literal expr_13C = this.SysLabel;
						expr_13C.Text += dataRow["Name"].ToString().Substring(0, 8);
					}
					else
					{
						Literal expr_171 = this.SysLabel;
						expr_171.Text += dataRow["Name"].ToString();
					}
					Literal expr_199 = this.SysLabel;
					expr_199.Text = expr_199.Text + "</div>" + Environment.NewLine;
				}
			}
		}
	}
	protected void ShowButton_Click(object sender, EventArgs e)
	{
		if (!string.IsNullOrEmpty(this.LabelName.Text) && !string.IsNullOrEmpty(this.TemplateContent.Text))
		{
			this.ShowPlace.Text = this.GetLabelHTML();
			this.ShowButton2.Text = "刷 新";
			this.ShowButton3.Text = "源 码";
		}
	}
	protected void ShowButton_Click2(object sender, EventArgs e)
	{
		if (this.ShowPlace.Text != "" && this.ShowPlace.Text != null)
		{
			if (this.ShowButton3.Text == "源 码")
			{
				this.ShowPlace.Text = base.Server.HtmlEncode(this.ShowPlace.Text);
				this.ShowButton3.Text = "效 果";
			}
			else
			{
				if (this.ShowButton3.Text == "效 果")
				{
					this.ShowPlace.Text = base.Server.HtmlDecode(this.ShowPlace.Text);
					this.ShowButton3.Text = "源 码";
				}
			}
		}
	}
	protected void PreStepButton_Click(object sender, EventArgs e)
	{
		if (!string.IsNullOrEmpty(Fetch.Get("action")) && !string.IsNullOrEmpty(Fetch.Get("name")))
		{
			int iD = Fetch.GetID("ID");
			if (Fetch.Get("action") == "add")
			{
				DataTable dataTable = (DataTable)this.Session[base.Request["name"]];
				if (dataTable.Rows[0]["OutType"].IsNull(new object[0]) == "programe")
				{
					base.Response.Redirect("Admin_LabelParms.aspx?action=add&id=0&name=" + base.Request["name"]);
				}
				else
				{
					base.Response.Redirect("Admin_LabelSqlBuild.aspx?action=add&id=0&name=" + base.Request["name"]);
				}
			}
			else
			{
				DataTable dataTable = this.lb.GetLabelDefineByLabelID(iD);
				if (dataTable.Rows[0]["OutType"].IsNull(new object[0]) == "programe")
				{
					base.Response.Redirect("Admin_LabelParms.aspx?action=modify&id=" + base.Request["id"] + "&name=" + base.Request["name"]);
				}
				else
				{
					base.Response.Redirect("Admin_LabelSqlBuild.aspx?action=modify&id=" + base.Request["id"] + "&name=" + base.Request["name"]);
				}
			}
		}
	}
	protected void FinishButton_Click(object sender, EventArgs e)
	{
		if (!string.IsNullOrEmpty(Fetch.Get("action")))
		{
			if (Fetch.Get("action") == "add" && this.Session[Fetch.Get("name")] != null)
			{
				DataTable dataTable = (DataTable)this.Session[Fetch.Get("name")];
				if (dataTable.Rows.Count > 0)
				{
					DataRow dataRow = dataTable.Rows[0];
					string name = dataRow["Name"].ToString();
					string type = dataRow["Type"].ToString();
					string intro = dataRow["LabelIntro"].ToString();
					string dataType = dataRow["LabelDataType"].ToString();
					string cacheTime = dataRow["CacheTime"].ToString();
					string outPutType = dataRow["LabelType"].ToString();
					string connStr = dataRow["ConnStr"].ToString();
					string text = this.TemplateContent.Text;
					string labelSqlString = dataRow["SqlStr"].ToString();
					string countPageSqlStr = dataRow["PageSqlStr"].ToString();
					string parmArray = dataRow["ParmArray"].ToString();
					string labelOutPutType = dataRow["OutPutType"].ToString();
					string labelStaticOutPut = dataRow["StaticOutPut"].ToString();
					if (this.lb.AddLabel(name, type, intro, dataType, cacheTime, outPutType, connStr, text, labelSqlString, countPageSqlStr, labelOutPutType, labelStaticOutPut, parmArray))
					{
						this.Session.Remove(Fetch.Get("name"));
						this.Session.Remove("Parm_Table_" + Fetch.Get("name"));
						this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Lable.aspx");
					}
				}
			}
			else
			{
				if (Fetch.Get("action") == "modify" && Fetch.GetID("ID") > 0)
				{
					int iD = Fetch.GetID("ID");
					string[,] parmArray2 = new string[0, 0];
					if (this.lb.UpdateLabel(iD, "", "", "", "", "", "", "", this.TemplateContent.Text, "", "", "", "", "", parmArray2))
					{
						base.Response.Redirect("Admin_Lable.aspx");
					}
					else
					{
						this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "未知"), "");
					}
				}
			}
		}
	}
	protected string GetLabelHTML()
	{
		string text;
		if (Fetch.Get("action") == "add" && Fetch.Get("name") != null)
		{
			DataTable dataTable = (DataTable)this.Session[Fetch.Get("name")];
			text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>";
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				for (int j = 0; j < dataTable.Columns.Count; j++)
				{
					text = string.Concat(new string[]
					{
						text,
						"<",
						dataTable.Columns[j].ColumnName.ToString(),
						">",
						dataTable.Rows[i][j].ToString(),
						"</",
						dataTable.Columns[j].ColumnName.ToString(),
						">\n"
					});
				}
			}
			text += "</root>";
			text = text.Replace("<ParmArray>", "");
			text = text.Replace("</ParmArray>", "");
		}
		else
		{
			text = "";
		}
		return Template.GetContentFormLabel(this.LabelName.Text, text, this.TemplateContent.Text, false);
	}
	protected void CreateLabel_Click(object sender, EventArgs e)
	{
		if (!string.IsNullOrEmpty(this.LabelName.Text) && !string.IsNullOrEmpty(this.TemplateContent.Text))
		{
			string text = Template.TransferLabelToHtml(this.LabelName.Text, false);
			string text2 = "";
			string text3 = "";
			string str = base.Server.MapPath("~/") + "\\Label\\LabelOutPut\\" + text2;
			if (base.Request["action"] != null && base.Request["action"] == "add" && base.Request["name"] != null)
			{
				DataTable dataTable = (DataTable)this.Session[base.Request["name"]];
				if (dataTable.Rows.Count > 0)
				{
					DataRow dataRow = dataTable.Rows[0];
					string text4 = dataRow["OutPutType"].ToString();
					text2 = dataRow["StaticOutPut"].ToString();
				}
				dataTable.Dispose();
			}
			else
			{
				if (Fetch.Get("action") == "modify" && Fetch.GetID("ID") > 0)
				{
					try
					{
						int iD = Fetch.GetID("ID");
						DataTable dataTable = this.lb.GetLabelByLabelID(iD);
						if (dataTable.Rows.Count == 1)
						{
							text2 = dataTable.Rows[0]["StaticOutPut"].ToString();
						}
						else
						{
							this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
						}
					}
					catch
					{
						this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
					}
				}
			}
			if (text2 != "")
			{
				string[] array = text2.Split(new char[]
				{
					'.'
				});
				if (array.Length == 2)
				{
					text3 = array[1];
				}
				FileIO fileIO = new FileIO();
				if (text3.ToLower() == "js")
				{
					text = text.Replace('\r'.ToString(), "");
					text = text.Replace('\n'.ToString(), "");
					text = "document.write(\"" + text.Replace("\"", "'") + "\");";
				}
				if (fileIO.CreateTemplateFile(text, str + "/" + text2))
				{
					this.WriteSuccessMsg("", "标签生成成功！", "Admin_Lable.aspx");
				}
				else
				{
					this.WriteErrMsg("", Language.Get("Common/Fail", "标签页已被其它进程访问或该标签没有写入权限！"), "");
				}
			}
		}
	}
}
